//
//  SelectAddressViewController.m
//  BiologyLibrary
//
//  Created by 朱进 on 2017/4/29.
//  Copyright © 2017年 youxh. All rights reserved.
//

#import "SelectAddressViewController.h"

@implementation SelectAddressViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviBarTitle: @"选择地址"];
    
    // 创建一个自定义的按钮，并添加到导航条左侧。
    UIButton *btnNaviLeft = [CustomNaviBarView createImgNaviBarBtnByImgNormal:@"bar_left_image"
                                                                 imgHighlight:@"bar_left_image_f"
                                                                       target:self
                                                                       action:@selector(btnNaviLeftClick)];
    [self setNaviBarLeftBtn:btnNaviLeft];
    
    // 创建一个自定义的按钮，并添加到导航条右侧。
    UIButton*_btnNaviRight = [CustomNaviBarView
                              createNormalNaviBarBtnByTitle:@"确定"
                              target:self
                              action:@selector(btnNaviRightClick)];
    [self setNaviBarRightBtn:_btnNaviRight];
    
    _addressDict = [NSDictionary dictionaryWithObjectsAndKeys:
                    @"A1",@"0", @"A2",@"1", @"A3",@"2", @"A4",@"3", @"A5",@"4", @"A6",@"5", @"A7",@"6", @"A8",@"7", @"A9",@"8",
                    @"B1",@"9", @"B2",@"10", @"B3",@"11", @"B4",@"12", @"B5",@"13", @"B6",@"14", @"B7",@"15", @"B8",@"16", @"B9",@"17",
                    @"C1",@"18", @"C2",@"19", @"C3",@"20", @"C4",@"21", @"C5",@"22", @"C6",@"23", @"C7",@"24", @"C8",@"25", @"C9",@"26",
                    @"D1",@"27", @"D2",@"28", @"D3",@"29", @"D4",@"30", @"D5",@"31", @"D6",@"32", @"D7",@"33", @"D8",@"34", @"D9",@"35",
                    @"E1",@"36", @"E2",@"37", @"E3",@"38", @"E4",@"39", @"E5",@"40", @"E6",@"41", @"E7",@"42", @"E8",@"43", @"E9",@"44",
                    @"F1",@"45", @"F2",@"46", @"F3",@"47", @"F4",@"48", @"F5",@"49", @"F6",@"50", @"F7",@"51", @"F8",@"52", @"F9",@"53",
                    @"G1",@"54", @"G2",@"55", @"G3",@"56", @"G4",@"57", @"G5",@"58", @"G6",@"59", @"G7",@"60", @"G8",@"61", @"G9",@"62",
                    @"H1",@"63", @"H2",@"64", @"H3",@"65", @"H4",@"66", @"H5",@"67", @"H6",@"68", @"H7",@"69", @"H8",@"70", @"H9",@"71",
                    @"I1",@"72", @"I2",@"73", @"I3",@"74", @"I4",@"75", @"I5",@"76", @"I6",@"77", @"I7",@"78", @"I8",@"79", @"I9",@"80",
                    nil];
    NSLog(@"_addressDict === %@",_addressDict);
    
    UIFont *inputFont = [UIFont flatFontOfSize:g_fSize_AppFont - 3];
    
    NSInteger iTFWidth = fWidth_Current / 8;
    NSInteger iTFHeight = 50;
    for (NSInteger i = 0; i < 8; i ++)
    {
        if (i < 7)
        {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:
                                       CGRectMake(0, 0, 5, iTFHeight)];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont flatFontOfSize:g_fSize_AppFont - 3];
            titleLabel.textAlignment = 1;
            titleLabel.textColor = [UIColor whiteColor];
            titleLabel.adjustsFontSizeToFitWidth = YES;
            
            switch (i) {
                case 0:
                    titleLabel.text = @"";
                    break;
                case 1:
                    titleLabel.text = @"-";
                    break;
                case 2:
                    titleLabel.text = @"-";
                    break;
                case 3:
                    titleLabel.text = @"-";
                    break;
                case 4:
                    titleLabel.text = @"-";
                    break;
                case 5:
                    titleLabel.text = @"-";
                    break;
                case 6:
                    titleLabel.text = @"-";
                    break;
                    
                default:
                    break;
            }
            
            UITextField *textField = [[UITextField alloc] initWithFrame:
                                      CGRectMake(iTFWidth * i,
                                                 fHeight_StatueBar + fHeight_Nav + 10,
                                                 iTFWidth, iTFHeight)];
            textField.tag = g_iTag_TextField + i;
            textField.delegate = self;
            textField.backgroundColor = [UIColor clearColor];
            textField.font = inputFont;
            textField.textColor = [UIColor whiteColor];
            textField.clearButtonMode = UITextFieldViewModeNever;
            textField.returnKeyType = UIReturnKeyDone;
            textField.leftViewMode = UITextFieldViewModeAlways;
            textField.leftView = titleLabel;
            textField.userInteractionEnabled = YES;
            textField.textAlignment = 1;
            [self.view addSubview:textField];
            textField.keyboardType = UIKeyboardTypeASCIICapable;

            CGRect tempRect = textField.frame;
            if(i == 0){
                textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

            }
            if (i == 1)
            {
                tempRect.origin.x = tempRect.origin.x;
                tempRect.size.width = tempRect.size.width - 10;
                textField.frame = tempRect;
                textField.adjustsFontSizeToFitWidth = YES;
                textField.keyboardType = UIKeyboardTypeNumberPad;

            }
            if (i == 2)
            {
                tempRect.origin.x = tempRect.origin.x -10;
                tempRect.size.width = tempRect.size.width;
                textField.frame = tempRect;
                textField.keyboardType = UIKeyboardTypeASCIICapable;
                textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

            }
            if (i == 3)
            {
                tempRect.origin.x = tempRect.origin.x ;
                tempRect.size.width = tempRect.size.width - 10;
                textField.frame = tempRect;
                textField.keyboardType = UIKeyboardTypeNumberPad;
            }
            if (i == 4)
            {
                tempRect.origin.x = tempRect.origin.x ;
                tempRect.size.width = tempRect.size.width - 10;
                textField.frame = tempRect;
                textField.keyboardType = UIKeyboardTypeNumberPad;
            }
            if (i == 5)
            {
                tempRect.origin.x = tempRect.origin.x ;
                tempRect.size.width = tempRect.size.width - 10;
                textField.frame = tempRect;
                textField.keyboardType = UIKeyboardTypeNumberPad;
            }
            if (i == 6)
            {
                textField.enabled = NO;
            }
        }
        else if ( i == 7)
        {
            UIButton *btnOK = [[UIButton alloc] initWithFrame:
                               CGRectMake(iTFWidth * i,
                                          fHeight_StatueBar + fHeight_Nav + 20,
                                          iTFWidth, iTFHeight - 20)];
            btnOK.backgroundColor = [UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
            btnOK.layer.cornerRadius = 10;
            [btnOK setTitle:@"查询" forState:UIControlStateNormal];
            [btnOK addTarget:self
                      action:@selector(searchClick:)
            forControlEvents:UIControlEventTouchUpInside];
            btnOK.titleLabel.font = inputFont;
            btnOK.clipsToBounds = YES;
            [self.view addSubview:btnOK];
        }
    }
    
    CGFloat fOriginX = 5;
    CGFloat fOriginY = fHeight_StatueBar + fHeight_Nav + 20 + iTFHeight;
    NSInteger iButtonWidth = (fWidth_Current - fOriginX *  2) / 9;
    NSInteger iButtonHeight = 30;
    
    for (NSInteger i = 0 ; i < 81; i ++)
    {
        UIButton *btnOK = [[UIButton alloc] initWithFrame:
                           CGRectMake(fOriginX + iButtonWidth * (i % 9),
                                      fOriginY + iButtonHeight * ( i / 9),
                                      iButtonWidth, iButtonHeight)];
        btnOK.tag = g_iTag_Button + i;
        btnOK.backgroundColor = [UIColor clearColor];
        btnOK.layer.borderWidth = 0.5;
        btnOK.layer.borderColor = [UIColor blackColor].CGColor;
        [btnOK setBackgroundImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateNormal];
        [btnOK setBackgroundImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateSelected];
        [btnOK setBackgroundImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateDisabled];
        [btnOK setTitle:[_addressDict objectForKey: [self getAddressKey:i]] forState:UIControlStateNormal];
        [btnOK addTarget:self
                  action:@selector(selectedClick:)
        forControlEvents:UIControlEventTouchUpInside];
        btnOK.titleLabel.font = inputFont;
        [self.view addSubview:btnOK];
    }
    
    _selectedAddressMArray = [[NSMutableArray alloc] init];
    [self getData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UserAction
-(void)btnNaviLeftClick
{
    
//    [self yanzhengguifan2];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)btnNaviRightClick
{
    if(!_selectedAddress || [_selectedAddress isEqualToString:@""])
    {
        [self.view makeToast:@"请选择地址"];
        return;
    }
    if (![self yanzhengguifan]) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectAddressFinished:withAddress:)])
    {
        [self.delegate didSelectAddressFinished:self withAddress:[self getTextFieldValue]];
    }
    
    [self btnNaviLeftClick];
}

-(void)getData{
    [self.view makeToastActivityWithMessage:@"获取最新地址"];
    self.view.userInteractionEnabled = NO;
    
    NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    self.strFOrN, @"addressType",nil];
    NSLog(@"parametersDict %@",parametersDict);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 15.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:[NSString stringWithFormat:@"%@getLastAddressList.do",APPDelegate.baseUrl]
       parameters:nil
          success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         [self.view hideToastActivityWithCompletion:nil];
         self.view.userInteractionEnabled = YES;
         
         if([responseObject isKindOfClass:[NSDictionary class]] &&
            [[responseObject allKeys] containsObject:@"error"])
         {
             [self.view hideToastActivityWithError:[responseObject objectForKey:@"error"]
                                        completion:nil];
             return ;
         }
         
         if ([[NSArray arrayWithArray:responseObject] count] > 1)
         {
             NSDictionary *tempDict = [[NSArray arrayWithArray:responseObject] objectAtIndex:0];
             _addressString = [tempDict objectForKey:@"mainAddress"];
             _selectedAddress = @"";
         }
         else
         {
             _addressString = [NSString stringWithFormat:@"%@-1-P%@A-11-1-1-", self.strFOrN, self.strFOrN];
             _selectedAddress = @"";
         }
         
         [self setTextFieldValue];

         [self getDetailData];
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         self.view.userInteractionEnabled = YES;
         
         DLog(@"operation %@",operation.responseString);
         if ([error code] == 1)
         {
             [self.view hideToastActivityWithError:d_Error_NetWork completion:nil];
         }
         else if([error code] == 2)
         {
             [self.view hideToastActivityWithError:d_Error_TimeOut completion:nil];
         }
         else
         {
             [self.view hideToastActivityWithError:d_Error_UnKnow completion:nil];
         }
     }];
}

- (void)getDetailData
{
    [self.view makeToastActivityWithMessage:@"获取详细地址"];
    self.view.userInteractionEnabled = NO;
    
    NSDictionary *parametersDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                    _addressString, @"mainAddress",nil];
    NSLog(@"parametersDict %@",parametersDict);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 设置超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 15.f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:[NSString stringWithFormat:@"%@getMAddressList.do",APPDelegate.baseUrl]
       parameters:parametersDict
          success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         self.view.userInteractionEnabled = YES;
         
         if([responseObject isKindOfClass:[NSDictionary class]] &&
            [[responseObject allKeys] containsObject:@"error"])
         {
             [self.view hideToastActivityWithError:[responseObject objectForKey:@"error"]
                                        completion:nil];
             return ;
         }
         
         [self clearSelectedAddress];
         if ([[NSArray arrayWithArray:responseObject] count] > 1)
         {
             for (NSDictionary *tempDict in [NSArray arrayWithArray:responseObject])
             {
                 [_selectedAddressMArray addObject:[tempDict objectForKey:@"address"]];
             }
         }
         
         [_selectedAddressMArray addObjectsFromArray:[APPDataCenter getusedaddressarr:_addressString]];
         
         [self setButtonEnabled];
         
         [self.view hideToastActivityWithCompletion:nil];
     }
          failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         self.view.userInteractionEnabled = YES;
         
         DLog(@"operation %@",operation.responseString);
         if ([error code] == 1)
         {
             [self.view hideToastActivityWithError:d_Error_NetWork completion:nil];
         }
         else if([error code] == 2)
         {
             [self.view hideToastActivityWithError:d_Error_TimeOut completion:nil];
         }
         else
         {
             [self.view hideToastActivityWithError:d_Error_UnKnow completion:nil];
         }
     }];
}

- (void)searchClick:(UIButton *)sender
{
    if (![self yanzhengguifan]) {
        return;
    }
    NSMutableString *mString = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i < 6; i ++)
    {
        UITextField *tempTextField = [self.view viewWithTag:g_iTag_TextField + i];
        [mString appendFormat:@"%@-",tempTextField.text];
    }
    _addressString = (NSString *)mString;
    [self getDetailData];
}

- (void)setTextFieldValue
{
    NSArray *addressArray = [_addressString componentsSeparatedByString:@"-"];
    for (NSInteger i = 0; i < 7; i ++)
    {
        UITextField *tempTextField = [self.view viewWithTag:g_iTag_TextField + i];
        tempTextField.text = [addressArray objectAtIndex:i];
        if (i == 6)
        {
            tempTextField.text = _selectedAddress;
        }
    }
}

- (NSString *)getTextFieldValue
{
    NSMutableString *mString = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i < 6; i ++)
    {
        UITextField *tempTextField = [self.view viewWithTag:g_iTag_TextField + i];
        [mString appendFormat:@"%@-",tempTextField.text];
    }
    [mString appendFormat:@"%@",_selectedAddress];
    
    return (NSString *)mString;
}

- (void)clearSelectedAddress
{
    _selectedKeyStr = @"";
    _selectedAddress = @"";
    for (NSString *keyStr in [_addressDict allKeys])
    {
        UIButton *tempButton = [self.view viewWithTag:g_iTag_Button + [keyStr integerValue]];
        if (tempButton.isSelected)
            tempButton.selected = NO;
    }
    [_selectedAddressMArray removeAllObjects];
}

- (void)setButtonEnabled
{
    for (NSString *keyStr in [_addressDict allKeys])
    {
        UIButton *tempButton = [self.view viewWithTag:g_iTag_Button + [keyStr integerValue]];
        if ([self isContainAddress:keyStr])
        {
            tempButton.enabled = NO;
        }
        else
        {
            tempButton.enabled = YES;
        }
    }
}

- (void)selectedClick:(UIButton *)sender
{
    [self.view endEditing:YES];

    UIButton *tempButton = [self.view viewWithTag:g_iTag_Button + [_selectedKeyStr integerValue]];
    tempButton.selected = NO;
    
    sender.selected = YES;
    _selectedKeyStr = [self getAddressKey:sender.tag - g_iTag_Button];
    _selectedAddress = [_addressDict objectForKey:_selectedKeyStr];
    UITextField *tempTextField = [self.view viewWithTag:g_iTag_TextField + 6];
    tempTextField.text = _selectedAddress;
}

- (BOOL) isContainAddress:(NSString *)keyStr
{
    for (NSString *address in _selectedAddressMArray)
    {
        if([address isEqualToString:[_addressDict objectForKey:keyStr]])
            return YES;
    }
    return NO;
}

- (NSString *) getAddressKey:(NSInteger )key
{
    for (NSString *keyStr in [_addressDict allKeys])
    {
        if ( [keyStr integerValue] == key)
        {
            return keyStr;
        }
    }
    return @"";
}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField.tag - g_iTag_TextField == 0)
    {
//        if (![textField.text hasPrefix:self.strFOrN])
//        {
//            textField.text = self.strFOrN;
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
//                                                                message:[NSString stringWithFormat:@"类型必须已%@开头",self.strFOrN]
//                                                               delegate:nil
//                                                      cancelButtonTitle:@"取消"
//                                                      otherButtonTitles:nil, nil];
//            [alertView show];
//            return NO;
//        }
        if (textField.text.length>0) {
            if(![self onlyInputACapital:textField.text]){
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                    message:[NSString stringWithFormat:@"类型必须为大写字母"]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:nil, nil];
                [alertView show];
                return NO;
            }
        }
    }
    else if (textField.tag - g_iTag_TextField == 1)
    {
        NSString *numberStr = textField.text;
        if ([numberStr integerValue] < 1)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于0"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    }
    
    else if (textField.tag - g_iTag_TextField == 2)
    {
        if (textField.text.length>0) {
            if(![self onlyInputACapital:textField.text]){
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                    message:[NSString stringWithFormat:@"架号必须为大写字母"]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"取消"
                                                          otherButtonTitles:nil, nil];
                [alertView show];
                return NO;
            }
        }
    }
    else if (textField.tag - g_iTag_TextField == 3)
    {
        NSString *numberStr = textField.text;
        if ([numberStr integerValue] < 11)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于10"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    }
//        if (![textField.text hasPrefix:[NSString stringWithFormat:@"P%@A", self.strFOrN]])
//        {
//            textField.text = [NSString stringWithFormat:@"P%@A", self.strFOrN];
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
//                                                                message:[NSString stringWithFormat:@"架号必须以P%@A开头", self.strFOrN]
//                                                               delegate:nil
//                                                      cancelButtonTitle:@"取消"
//                                                      otherButtonTitles:nil, nil];
//            [alertView show];
//            return NO;
//        }
        
    
    
    else
    {
        if ([textField.text integerValue] < 1)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于0"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    }
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//    if (textField.tag - g_iTag_TextField == 0)
//    {
//        if ([textField.text length] < 2 && string.length == 0)
//        {
//            textField.text = self.strFOrN;
//            return NO;
//        }
//    }
//    else if (textField.tag - g_iTag_TextField == 1)
//    {
//        if ([textField.text length] < 4  && string.length == 0)
//        {
//            textField.text = [NSString stringWithFormat:@"P%@A", self.strFOrN];
//            return NO;
//        }
//    }
    
    return YES;
}
-(BOOL)yanzhengguifan{
    UITextField*fieldone=(UITextField *) [self.view viewWithTag:g_iTag_TextField];
    UITextField*fieldtwo=(UITextField *) [self.view viewWithTag:g_iTag_TextField+1];
    UITextField*fieldthree=(UITextField *) [self.view viewWithTag:g_iTag_TextField+2];
    UITextField*fieldfour=(UITextField *) [self.view viewWithTag:g_iTag_TextField+3];
    UITextField*fieldfive=(UITextField *) [self.view viewWithTag:g_iTag_TextField+4];
    UITextField*fieldsix=(UITextField *) [self.view viewWithTag:g_iTag_TextField+5];

    NSLog(@"%@,%@,%@,%@,%@,%@",fieldone.text,fieldtwo.text,fieldthree.text,fieldfour.text,fieldfive.text,fieldsix.text);
    
    if (fieldone.text.length>0) {
        if(![self onlyInputACapital:fieldone.text]){
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[NSString stringWithFormat:@"类型必须为大写字母"]
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    }
//        if (![fieldone.text hasPrefix:self.strFOrN])
//        {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
//                                                                message:[NSString stringWithFormat:@"类型必须已%@开头",self.strFOrN]
//                                                               delegate:nil
//                                                      cancelButtonTitle:@"取消"
//                                                      otherButtonTitles:nil, nil];
//            [alertView show];
//            return NO;
//        }
    
        NSString *numberStr = fieldtwo.text;
        if ([numberStr integerValue] < 1)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于0"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    
    if (fieldthree.text.length>0) {
        if(![self onlyInputACapital:fieldthree.text]){
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[NSString stringWithFormat:@"架号必须为大写字母"]
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    }
    
 
//        if (![fieldtwo.text hasPrefix:[NSString stringWithFormat:@"P%@A", self.strFOrN]])
//        {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
//                                                                message:[NSString stringWithFormat:@"架号必须以P%@A开头", self.strFOrN]
//                                                               delegate:nil
//                                                      cancelButtonTitle:@"取消"
//                                                      otherButtonTitles:nil, nil];
//            [alertView show];
//            return NO;
//        }
    
        NSString *numberStr2 = fieldfour.text;
        if ([numberStr2 integerValue] < 11)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于10"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    
    
    
        if ([fieldfive.text integerValue] < 1||[fieldsix.text integerValue] < 1)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"必须大于0"
                                                               delegate:nil
                                                      cancelButtonTitle:@"取消"
                                                      otherButtonTitles:nil, nil];
            [alertView show];
            return NO;
        }
    
    
    return YES;


}
-(void)yanzhengguifan2{
    UITextField*fieldone=(UITextField *) [self.view viewWithTag:g_iTag_TextField];
    UITextField*fieldtwo=(UITextField *) [self.view viewWithTag:g_iTag_TextField+1];
    UITextField*fieldthree=(UITextField *) [self.view viewWithTag:g_iTag_TextField+2];
    UITextField*fieldfour=(UITextField *) [self.view viewWithTag:g_iTag_TextField+3];
    
    
    if (![fieldone.text hasPrefix:self.strFOrN])
    {
        fieldone.text=[NSString stringWithFormat:@"%@1",self.strFOrN];
        
    }
    
    NSString *numberStr = [fieldone.text substringFromIndex:1];
    if ([numberStr integerValue] < 1)
    {
        fieldone.text=[NSString stringWithFormat:@"%@1",self.strFOrN];
    }
    
    
    if (![fieldtwo.text hasPrefix:[NSString stringWithFormat:@"P%@A", self.strFOrN]])
    {
        fieldtwo.text=[NSString stringWithFormat:@"P%@A11",self.strFOrN];
       
    }
    
    NSString *numberStr2 = [fieldtwo.text substringFromIndex:1];
    if ([numberStr2 integerValue] < 11)
    {
        fieldtwo.text=[NSString stringWithFormat:@"P%@A11",self.strFOrN];

    }
    
    
    
    if ([fieldthree.text integerValue] < 1||[fieldfour.text integerValue] < 1)
    {
        fieldthree.text=@"1";
        fieldfour.text=@"1";
    }
    
    
    
    
}
- (BOOL)onlyInputACapital:(NSString*)string{
    NSString *regex =@"[A-Z]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    BOOL inputString = [predicate evaluateWithObject:string];
    return inputString;
}
@end
